package keeto.com.vn.bms.ui.adapter;

import java.util.ArrayList;
import keeto.com.vn.bms.model.report.ReportDoctorModel;
import keeto.com.vn.bms.ui.activity.R;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class ListMultiChoiceDoctorAdapter extends BaseAdapter {

	private Context  ctx;
	private LayoutInflater mInflater;
	private ArrayList<ReportDoctorModel> mArrayList;
	private String Strname="";
	private Button btnDoctor;
	public ListMultiChoiceDoctorAdapter( Context context,ArrayList<ReportDoctorModel> arrayList,Button button) {
		this.ctx = context;
		this.mArrayList = arrayList;
		this.mInflater = LayoutInflater.from(context);
		this.btnDoctor = button;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mArrayList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return getItem(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder holder;
		if (convertView == null){
			convertView = mInflater.inflate(R.layout.item_checkbox_layout,parent,false);
			holder = new ViewHolder();
			holder.cBox = (CheckBox)convertView.findViewById(R.id.ckb_item);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		holder.cBox.setText(mArrayList.get(position).getDoctorName());
		holder.cBox.setChecked(mArrayList.get(position).isSelected());
		Strname ="";
		holder.cBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				ReportDoctorModel model = mArrayList.get(position);
				model.setSelected(isChecked);
				if(isChecked ==  true){
					Strname = Strname + holder.cBox.getText().toString()+ ", ";
				} else {
					Strname = Strname.replace(holder.cBox.getText().toString()+", ", "");
				}
				
				btnDoctor.setText(Strname);
				
			}
		});
//		holder.cBox.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				Strname ="";
//				ReportDoctorModel model = mArrayList.get(position);
//				if (holder.cBox.isChecked()){
//					holder.cBox.setChecked(false);
//					model.setSelected(false);
//				}else{
//					holder.cBox.setChecked(true);
//					model.setSelected(true);
//
//				}
//				notifyDataSetChanged();
//				for (int i =0;i<mArrayList.size();i++){
//					if (mArrayList.get(i).isSelected()){
//						Strname += mArrayList.get(i).getDoctorName()+ ", ";
//					}
//				}
//				if (Strname.length()>2){
//					btnDoctor.setText(Strname.subSequence(0, Strname.length()-2));
//				}else{
//					btnDoctor.setText(ctx.getResources().getString(R.string.detail_doctor_des));
//				}
//			}
//		});

		return convertView;
	}

	class ViewHolder{
		private CheckBox cBox;
	}



}
